package cn.xfdzx.android.apps.shop.activity.myshop;


import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.WithdrawalListAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.SubmitWithdrawalBeen;
import cn.xfdzx.android.apps.shop.bean.WithdrawalListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.ItemLengthListener;
import cn.xfdzx.android.apps.shop.contract.WithdrawalListContract;
import cn.xfdzx.android.apps.shop.presenter.WithdrawalListPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;

/**
 * 提现列表
 */

public class WithdrawalListActivity extends BaseMvpActivity<WithdrawalListPresenter> implements WithdrawalListContract.View, ItemClickListener {


    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.ll_hint)
    LinearLayout mHint;
    @BindView(R.id.ll_num)
    LinearLayout llNum;
    @BindView(R.id.tv_all_select)
    TextView tvAllSelect;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_total_price)
    TextView tvTotalPrice;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    private int page = 1;
    private int limit = Constants.LIMIT;
    private String storeId;
    private WithdrawalListAdapter withdrawalListAdapter;
    private List<WithdrawalListBeen.DataBean.OrderListBean> mList = new ArrayList<>();

    private int type = 0;
    private boolean is_select_all = false;
    private String ids = "";

    private double totalMoney = 0;//全选的总价
    private int totalNum = 0;//全选的总数
    private double total_price = 0;//选择的总价
    private int total_num = 0;//选择的总数


    @Override
    public int getLayoutId() {
        return R.layout.activity_withdrawal_list;
    }

    @Override
    public void initView() {
        mPresenter = new WithdrawalListPresenter();
        mPresenter.attachView(this);
        type = getIntent().getIntExtra("type", 0);
        storeId = UtilsData.getPreference(this, "login").getString("storeId");
//        storeId = "131351";
        mPresenter.withdrawalList(true, true, storeId, page + "", limit + "");

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvList.setLayoutManager(linearLayoutManager);
        withdrawalListAdapter = new WithdrawalListAdapter(this, mList, type, this);

        rvList.setAdapter(withdrawalListAdapter);

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                total_price = 0;
                total_num = 0;
                is_select_all = false;
                Drawable nav_up = getResources().getDrawable(R.drawable.d99999_oval);
                nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
                llNum.setVisibility(View.GONE);
                page = 1;
                mPresenter.withdrawalList(true, false, storeId, page + "", limit + "");
            }
        });

        if (type == 0) {
            tvSubmit.setBackgroundResource(R.drawable.dfb9129_bg_r3);
        } else {
            tvSubmit.setBackgroundResource(R.drawable.d02c557_bg_r3);
        }

    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }


    @Override
    public void onWithdrawalListSuccess(WithdrawalListBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            totalMoney = bean.getData().getCash_out_money();
            totalNum = bean.getData().getTotal();
            if (page == 1) {
                mList.clear();
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.withdrawalList(false, false, storeId, page + "", limit + "");
                    }
                });
            }
            if (bean.getData().getOrder_list().size() == 0) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
            }

            for (WithdrawalListBeen.DataBean.OrderListBean orderListBean: bean.getData().getOrder_list()){

                if (orderListBean.getOrder_status()==0){
                    orderListBean.setIs_select(false);
                }else {
                    orderListBean.setIs_select(true);
                }
                mList.add(orderListBean);
            }


            withdrawalListAdapter.notifyDataSetChanged();

            if (mList.size() == 0) {
                mHint.setVisibility(View.VISIBLE);
            } else {
                mHint.setVisibility(View.GONE);
            }
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onSubmitWithdrawalSuccess(SubmitWithdrawalBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Intent intent = new Intent(this, WithdrawalDetailsActivity.class);
            intent.putExtra("type", type);
            intent.putExtra("data", bean.getData());
            startActivity(intent);
            finish();

        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    @Override
    protected void onDestroy() {
        mList.clear();
        mList = null;
        super.onDestroy();
    }

    @OnClick({R.id.iv_back, R.id.tv_all_select, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:  //返回按钮点击
                finish();
                break;
            case R.id.tv_all_select:  //
                is_select_all = !is_select_all;
                ids = "";
                if (is_select_all) {
                    for (WithdrawalListBeen.DataBean.OrderListBean orderListBean : mList) {
                        if (orderListBean.getOrder_status() == 0) {
                            orderListBean.setIs_select(true);
                        }
                    }
                    if (type == 0) {
                        Drawable nav_up = getResources().getDrawable(R.drawable.icon_select_orange);
                        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                        tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
                    } else {
                        Drawable nav_up = getResources().getDrawable(R.drawable.icon_select_green);
                        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                        tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
                    }
                    tvNum.setText(totalNum + "");
                    tvTotalPrice.setText(totalMoney + "");
                    llNum.setVisibility(View.VISIBLE);
                } else {
                    for (WithdrawalListBeen.DataBean.OrderListBean orderListBean : mList) {
                        if (orderListBean.getOrder_status() == 0) {
                            orderListBean.setIs_select(false);
                        }
                    }
                    Drawable nav_up = getResources().getDrawable(R.drawable.d99999_oval);
                    nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                    tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
                    llNum.setVisibility(View.GONE);
                }

                withdrawalListAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_submit:
                ids = "";
                String is_full;
                if (is_select_all) {
                    is_full = "1";
                    for (WithdrawalListBeen.DataBean.OrderListBean orderListBean : mList) {
                        if (orderListBean.getOrder_status() == 0) {

                            if (!orderListBean.isIs_select()) {
                                if (ids.isEmpty()) {
                                    ids = orderListBean.getOrder_sn();
                                } else {
                                    ids = ids + "," + orderListBean.getOrder_sn();
                                }
                            }
                        }
                    }
                } else {
                    is_full = "0";
                    for (WithdrawalListBeen.DataBean.OrderListBean orderListBean : mList) {
                        if (orderListBean.getOrder_status()==0) {

                        if (orderListBean.isIs_select()) {
                            if (ids.isEmpty()) {
                                ids = orderListBean.getOrder_sn();
                            } else {
                                ids = ids + "," + orderListBean.getOrder_sn();
                            }
                        }}
                    }
                }

                mPresenter.submitWithdrawal(storeId, is_full, "1", ids, "");
                break;
        }
    }

    @Override
    public void onItemClick(int position) {
        if (mList.get(position).getOrder_status() != 0) {
            return;
        }
        boolean is_all = true;

        if (is_select_all) {
            total_price = totalMoney;
            total_num = totalNum;
            for (WithdrawalListBeen.DataBean.OrderListBean orderListBean : mList) {
                if (orderListBean.getOrder_status()==0) {

                if (!orderListBean.isIs_select()) {
                    is_all = false;

                    total_price = Utils.subDouble(total_price, orderListBean.getPay_price());
                    total_num--;
                }}
            }
            if (is_all) {
                if (type == 0) {
                    Drawable nav_up = getResources().getDrawable(R.drawable.icon_select_orange);
                    nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                    tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
                } else {
                    Drawable nav_up = getResources().getDrawable(R.drawable.icon_select_green);
                    nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                    tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
                }

                tvNum.setText(totalNum + "");
                tvTotalPrice.setText(totalMoney + "");
                llNum.setVisibility(View.VISIBLE);
            } else {
                if (total_price != 0) {
                    tvNum.setText(total_num + "");
                    tvTotalPrice.setText(total_price + "");
                    llNum.setVisibility(View.VISIBLE);
                    if (type == 0) {
                        Drawable nav_up = getResources().getDrawable(R.drawable.icon_select_all_orange);
                        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                        tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
                    } else {
                        Drawable nav_up = getResources().getDrawable(R.drawable.icon_select_all_green);
                        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                        tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
                    }
                } else {
                    is_select_all = false;
                    llNum.setVisibility(View.GONE);
                    Drawable nav_up = getResources().getDrawable(R.drawable.d99999_oval);
                    nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                    tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
                }
            }
        } else {
            total_price = 0;
            total_num = 0;
            for (WithdrawalListBeen.DataBean.OrderListBean orderListBean : mList) {
                if (orderListBean.getOrder_status()==0) {
                    if (!orderListBean.isIs_select()) {
                        is_all = false;
                    } else {
                        total_price = Utils.addDouble(total_price, orderListBean.getPay_price());
                        total_num++;
                    }
                }
            }
            if (total_price == totalMoney) {
                is_select_all = true;
            }
            if (is_all) {
                tvNum.setText(totalNum + "");
                tvTotalPrice.setText(totalMoney + "");
                llNum.setVisibility(View.VISIBLE);
                if (type == 0) {
                    Drawable nav_up = getResources().getDrawable(R.drawable.icon_select_orange);
                    nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                    tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
                } else {
                    Drawable nav_up = getResources().getDrawable(R.drawable.icon_select_green);
                    nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                    tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
                }
            } else {
                if (total_num == 0) {
                    llNum.setVisibility(View.GONE);
                } else {
                    llNum.setVisibility(View.VISIBLE);
                }
                tvNum.setText(total_num + "");
                tvTotalPrice.setText(total_price + "");

                Drawable nav_up = getResources().getDrawable(R.drawable.d99999_oval);
                nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
            }
        }
    }


}
